% 读取图像并灰度化
image = imread('pic_measure.png');
gray_image = rgb2gray(image);

% 平滑处理：尝试使用高斯模糊减少噪声
sigma = 2;
smoothed_image = imgaussfilt(gray_image, sigma);

% 边缘检测（可尝试不同算子）
edges_canny = edge(smoothed_image, 'Canny', [0.1, 0.3]); % 调整阈值
edges_sobel = edge(smoothed_image, 'Sobel');

% 形态学处理：膨胀与闭运算
se = strel('disk', 2); % 定义结构元素
edges_canny_morph = imclose(edges_canny, se); % 闭运算
edges_sobel_morph = imclose(edges_sobel, se); % 闭运算

% 显示处理结果
figure;
subplot(2, 2, 1);
imshow(edges_canny);
title('Canny Original');

subplot(2, 2, 2);
imshow(edges_canny_morph);
title('Canny After Morphology');

subplot(2, 2, 3);
imshow(edges_sobel);
title('Sobel Original');

subplot(2, 2, 4);
imshow(edges_sobel_morph);
title('Sobel After Morphology');

% 保存结果
imwrite(edges_canny_morph, 'edges_canny_morph.png');
imwrite(edges_sobel_morph, 'edges_sobel_morph.png');
